package com.wyp168.leetcode.listnode;

/**
 * @ClassName ReverseList
 * @Description 链表反转
 * @Author wyp168
 * @Date 2022/6/21 21:00
 */
public class ReverseList {
    public static void main(String[] args) {
        ListNode ln1 = new ListNode(1);
        ListNode ln2 = new ListNode(2);
        ListNode ln3 = new ListNode(3);
        ln1.next = ln2;
        ln2.next = ln3;

        System.out.println(reverseList(ln1));
    }
    
    
    /** 
     * @description: 通过递归方式实现链表反转
     * @author wyp168
     * @date: 2022/6/21 21:00
     */
    public static ListNode reverseList(ListNode head) {
        //递归出口
       if(head == null || head.next == null) {
           return head;
       }
       
       //记录当前节点的下一个节点
        ListNode next = head.next;

        ListNode listNode = reverseList(next);

        next.next = head;

        head.next = null;

        return listNode;

    }
}
